import 'dart:convert';

import '../constants/constants.dart';
import '../constants/task_status.dart';

class TaskEntity {
  int? id;
  String taskId;
  String url;
  String fileName;
  int fileSize;
  String filePath;
  String status;
  String? group;
  int? createdAt;
  double? progress;
  String? errorCode;
  String? errorMessage;

  TaskEntity({
    this.id,
    this.taskId = '',
    this.url = '',
    this.fileName = '',
    this.fileSize = 0,
    this.filePath = '',
    this.status = 'enqueued',
    this.group,
    this.createdAt,
    this.progress,
    this.errorCode,
    this.errorMessage,
  });

  factory TaskEntity.fromJson(Map<String, dynamic> json) {
    return TaskEntity(
      id: json[columnId],
      taskId: json['taskId'],
      url: json['url'],
      fileName: json['fileName'],
      fileSize: json['fileSize'],
      filePath: json['filePath'],
      status: json['status'],
      group: json['group'],
      createdAt: json['createdAt'],
      progress: json['progress'] ?? 0.0,
      errorCode: json['errorCode'],
      errorMessage: json['errorMessage'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data[columnId] = id;
    data['taskId'] = taskId;
    data['url'] = url;
    data['fileName'] = fileName;
    data['fileSize'] = fileSize;
    data['filePath'] = filePath;
    data['status'] = status;
    data['group'] = group;
    data['createdAt'] = createdAt;
    data['progress'] = progress;
    data['errorCode'] = errorCode;
    data['errorMessage'] = errorMessage;
    return data;
  }

  toMap() {
    if (id != null) {
      return toJson();
    } else {
      return toJson()..remove(columnId);
    }
  }

  fromMap() {
    toJson();
  }

  @override
  String toString() {
    return jsonEncode(this);
  }
}
